<?
define(EXCLUDE_QUERY, " AND driver_id != '16' AND driver_id != '100' AND driver_id != '101'");
class PointsAdd {
	var $driverid;
	var $adjustmentReason;
	var $points;
	var $resetFields;
	var $sendEmail;
	var $sendMessage;
	var $emailSubject;
	var $emailBody;
	var $errors; 
	var $entryBy;
	
	function displayActiveDriverList($selected = ""){
		$query = "SELECT * FROM t_drivers WHERE (active = '1' AND durational = '0'" . EXCLUDE_QUERY . ") ORDER BY lastname ASC";
		$result = mysql_query($query);
		echo "<select name=\"driver\" onchange=\"updateExtraFields()\">\n";
		while ($data = mysql_fetch_array($result)){
			echo "<option value=\"" . $data['driver_id'] . "\"";
			if ($selected == $data['driver_id']) echo " selected=\"selected\"";
			echo ">" . $data['lastname'] . ", " . $data['firstname'] . "</option>\n";
		}
		echo "</select>\n";
	}

	function displayActiveDriverJavascript(){
		echo "<script type=\"text/javascript\">\n";
		echo "var drivers = new Array();\n";
		$query = "SELECT * FROM t_drivers WHERE (active = '1' AND durational = '0'" . EXCLUDE_QUERY . ") ORDER BY lastname ASC";
		$result = mysql_query($query);
		while ($data = mysql_fetch_array($result)){
			echo "drivers[" . $data['driver_id'] . "] = \"" . $data['firstname'] . "\";\n";
		}
		echo "</script>\n";

	}
	
	function getData(){
		$this->driverid = $_POST['driver'];
		$this->adjustmentReason = $_POST['reason'];
		$this->points = $_POST['points'];
		$this->sendEmail = $_POST['sendEmail'];
		$this->sendMessage = $_POST['sendMessage'];
		$this->resetFields = $_POST['resetFields'];
		$this->emailSubject = $_POST['emailSubject'];
		$this->emailBody = $_POST['emailBody'];
	}
	
	function validateData(){
		if($this->entryBy <= 0) $this->errors["noEntryBy"] = true;
		if (empty($this->adjustmentReason)) $this->errors['noReason'] = "Please enter the reason for the adjustment.";
		if ($this->points == "0") $this->errors['noPoints'] = "The points value cannot equal 0.";
		if (!is_numeric($this->points)) $this->errors['notNumeric'] = "Points must be numerical.";
		if ($this->sendEmail || $this->sendMessage){
			if (empty($this->emailSubject)) $this->errors['noSubject'] = "You must enter a subject for the email/message.";
			if (empty($this->emailBody)) $this->errors['noBody'] = "You must enter a body for the email/message.";
		}
		if (empty($this->errors)) return true;
		else return false;
	}
	
	function insertData(){
		$query = "INSERT INTO t_seniority SET driver_id = '" . $this->driverid . "', points = '" . $this->points . "', entry_by = '" . $this->entryBy . "', description = " . quote_smart($this->adjustmentReason);
		$result = @mysql_query($query);
		$msg = "";
		if ($result) {
			$msg .= "Points have successfully been added.\n<br>";
			if ($this->sendMessage){
				$messageQuery = "INSERT INTO t_messages SET from_id = '" . $this->entryBy . "', to_id = '" . $this->driverid . "', subject = " . quote_smart($this->emailSubject) . ", message = " . quote_smart(nl2br($this->emailBody));
				$messageResult = @mysql_query($messageQuery);
				if ($messageResult) $msg .= "Message successfully sent to driver.\n<br>";
				else $msg .= "Error sending message to driver. If this error continues, please contact an administrator.";
			}
			if ($this->sendEmail){
				$getEmailQuery = "SELECT email FROM t_drivers WHERE driver_id = '" . $this->driverid . "'";
				$getEmailResult = mysql_query($getEmailQuery);
				if (mysql_num_rows($getEmailResult) == 1) {
					$driverEmail = mysql_result($getEmailResult, 0);
					if (!empty($driverEmail)) {
						if (@mail($driverEmail, $this->emailSubject, $this->emailBody, "From: transpo@web.uconn.edu")) $msg .= "Email successfully sent to " . $driverEmail . ".\n<br>";
						else $msg .= "Error sending email to driver. If this error continues, please contact an administrator.";
					} else $msg .= "Email not sent. No email address for this driver.";
				}
			}
		} else $msg .= "An error has occured. Please try again. If this error persists, please contact an administrator.";
		
		if ($this->resetFields) $this->resetData();
		
		return $msg;
	}
	
	function getErrors(){
		if ($this->errors) return $this->errors;
		else return false;
	}
	
	function resetData(){
		unset($this->driverid);
		unset($this->adjustmentReason);
		unset($this->points);
		unset($this->sendEmail);
		unset($this->sendMessage);
		unset($this->emailSubject);
		unset($this->emailBody);
	}
	
	function showErrors(){
		echo "<ul>\n";
		foreach($this->errors AS $error)
			echo "<li>" . $error . "</li>\n";
		echo "</ul>";
	}
	
	function setEnteredBy($id) { $this->entryBy = $id; }
	
	function getDriverID(){ return $this->driverid; }
	function getReason() { return $this->adjustmentReason; }
	function getPoints() { 
		if ($this->points == "" || $this->points == "0") return "0.00";
		else return $this->points;
	}
	function getResetFields() { if($this->resetFields) return " checked"; }
	function getSendMessage() { if($this->sendMessage) return " checked"; }
	function getSendEmail() { if ($this->sendEmail) return " checked"; }
	function getSubject() { return $this->emailSubject; }
	function getBody() { return $this->emailBody; }
	
}
?>